<script>
	import service from './service.js';
	import Version from './notification/version.js'
	import {
		undateTaskImg,
		getVersion
	} from './api/request.js';
	export default {
		onLaunch: function() {
			console.log('App Launch');
			if(uni.getStorageSync('netType')){
				
			} else {
				uni.setStorageSync('netType', false)
			}
			// console.log('<>>>>>>>>>>>>>>>>>>>>>>>> ', uni.getStorageSync('netType'))
			setInterval(() => {
				if(!uni.getStorageSync('tobacco_carID')){
					return
				}
				uni.getLocation({
					type: 'gcj02',
					"geocode": true,
					success: function(res) {
						uni.request({
							url: 'http://ccwx.jltobacco.com.cn:4000/yancaoMap/app/saveLatAndLng',
							method: 'POST',
							data: {
								carId: uni.getStorageSync('tobacco_carID'),
								lngAndLat: res.longitude + "|" + res.latitude
							},
							success: (res) => {
								// console.warn("坐标回传成功", res)
							},
							fail: err => {
								console.error(err)
							}
						})
					},
					fail: (err) => {
						console.error(err)
					}
				});
			}, 30000)
		},
		onShow: async function() {
			//版本更新
			let versionCode = plus.runtime.versionCode
			
			let update = await getVersion({"version_code":versionCode,"create_user_id":0})
			console.log(update)
			if (update.map.data !== false){
				Version.downloadApkAndShowProgressForUpdate(update.map.data.url);
			}
			// setTimeout(() => {
			// 	let user = service.getUsers().user;
			// 	if (user != null && user != undefined) {
			// 		if (this.$store.state.is_open_socket == false) {
			// 			this.$store.commit("connectSocketInit", user.id)
			// 		}
			// 	}
			// }, 1500)

			clearInterval(getApp().globalData.taskTimer);

			getApp().globalData.taskTimer = setInterval(async () => {
				if (uni.getStorageSync('catch') && getApp().globalData.ifTaskTimer){
					let allShopInfo = uni.getStorageSync('catch').data.map.allShopInfo
					let doStatus = false
					for (let i in allShopInfo) {
						let e = allShopInfo[i]
						if (e.deliveryStatus === 5) {
							getApp().globalData.ifTaskTimer = false
							e.task.receiveUserName = uni.getStorageSync(e.task.stroageName) //
							await undateTaskImg(e.task).then(res => {
								if (res.code == 200) {
									let tmp = uni.getStorageSync('catch')
									uni.removeStorageSync(e.task.receiveUserName) //
									tmp.data.map.allShopInfo[e.i].task = ''
									tmp.data.map.allShopInfo[e.i].deliveryStatus = 6
									uni.setStorageSync('catch', tmp)
									doStatus = true
								}
							})
						}
					}
					if(doStatus){
						clearCache()
						uni.$emit('RefreshList')
					}
				}
			}, 10000)
		},
		onHide: function() {
			console.log('App Hide');
		},
		onTabItemTap: function(e) {
			console.log(e);
			// e的返回格式为json对象： {"index":0,"text":"首页","pagePath":"pages/index/index"}
		},
		globalData: {
			taskTimer: '',
			ifTaskTimer:true, //getApp().globalData.ifTaskTimer
			
		}
	}
	function clearCache() {
		let that = this;
		let os = plus.os.name;
		if (os == 'Android') {
			let main = plus.android.runtimeMainActivity();
			let sdRoot = main.getCacheDir();
			let files = plus.android.invoke(sdRoot, "listFiles");
			let len = files.length;
			for (let i = 0; i < len; i++) {
				let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
				plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
					if (entry.isDirectory) {
						entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
							// uni.showToast({
							// 	title: '缓存清理完成',
							// 	duration: 2000
							// });
							// that.formatSize(); // 重新计算缓存  
						}, function(e) {
							console.log(e.message)
						});
					} else {
						entry.remove();
					}
				}, function(e) {
					console.log('文件路径读取失败')
				});
			}
		}
	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	@import './common/uni.css';

	/* #endif */
	/* #ifndef APP-NVUE */
	body {
		background-color: rgba(243, 243, 243, 1);
		// background-color: red;
	}

	/* #endif */
	/*每个页面公共css */
	/* uni-app默认全局使用flex布局。因为flex布局有利于跨更多平台，尤其是采用原生渲染的平台。如不了解flex布局，请参考http://www.w3.org/TR/css3-flexbox/。如不使用flex布局，请删除或注释掉本行。*/
	/* 按钮水平居中 */
	/* uni-input *{height: 100%;align-items: center;display: inline-flex;}
	uni-page-head .uni-btn-icon{margin: 0 auto !important;}
	.popup-right .popup-time-price uni-radio .uni-radio-input{
		border: none !important;
	}
	.uni-navbar .navbar-search input{border-radius: 20upx;}
	.choose-receiving-address .uni-navbar-header .uni-navbar-header-btns:first-child{padding-left: 0 !important;}
	.choose-receiving-address .uni-navbar-header .uni-navbar-header-btns:last-child{padding-right: 0 !important;}
	.add-address .uni-radio-input{
		width: 28upx !important;
		height: 28upx !important;
	}
	.add-address .tags .uni-radio-input, .add-address .tags .uni-radio-wrapper{
		width: 0 !important;
		height: 0 !important;
		border: none;
		display: none;
	}
	.add-address .tags .uni-radio-input:before{
		display: none;
	}
	.add-address .address .uni-input-input{
		width: 85%;
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
	}
	
	.tags radio-group label{
		&>uni-view:first-child{display: none !important;}
	}
	
	// 自定义导航
	@import "common/scss/_navbar.scss"; */
	// 选择位置导航
	.system-header {
		background-color: #96E7BA !important;
	}

	/* .hasNoData{
		position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: url('static/img/icon/null.png') center no-repeat; width: 100%; height: 100%; background-color: red; background-size: contain;
	} */
</style>
